package com.basic.business.store.service.impl;

import com.basic.business.store.vo.StoreLeaderRespVo;
import com.basic.common.constant.CacheConstants;
import com.basic.common.core.domain.entity.SysUser;
import com.basic.common.core.redis.RedisCacheUtils;
import com.basic.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.basic.common.core.service.impl.BaseServiceImpl;
import com.basic.business.store.domain.TStoreMgmt;
import com.basic.business.store.service.ITStoreMgmtService;
import com.basic.business.store.mapper.TStoreMgmtMapper;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 店铺管理Service业务层处理
 *
 * @author zhaoxy_jn@163.com
 */
@Service
public class TStoreMgmtServiceImpl extends BaseServiceImpl<TStoreMgmtMapper,TStoreMgmt> implements ITStoreMgmtService {

    @Resource
    private TStoreMgmtMapper toreMgmtMapper;

    /**
     * 项目启动时，初始化店铺信息缓存
     */
    @PostConstruct
    public void init(){
        resetStoreCache();
    }

    @Override
    public List<Map<String, Object>> getStoreIdListByUserId(Long userId) {
        return toreMgmtMapper.getStoreIdListByUserId(userId);
    }

    @Override
    public List<TStoreMgmt> getDoesItExist(TStoreMgmt tStoreMgmt) {
        return toreMgmtMapper.getDoesItExist(tStoreMgmt);
    }

    @Override
    public List<TStoreMgmt> getNormalStoreMgmtList() {
        return toreMgmtMapper.getNormalStoreMgmtList();
    }

    @Override
    public int updateStatusByIds(String[] ids, String status) {
        return toreMgmtMapper.updateStatusByIds(ids, status);
    }

    @Override
    public void resetStoreCache() {
        // 清空缓存
        RedisCacheUtils.clearCache(CacheConstants.STORE_REDIS_KEY + "*");
        // 加载缓存
        loadingStoreCache();
    }

    /**
     * 加载店铺信息缓存
     */
    private void loadingStoreCache() {
        List<TStoreMgmt> storeList = toreMgmtMapper.getNormalStoreMgmtList();
        if (storeList == null || storeList.isEmpty()) {
            return;
        }

        // 处理ID映射
        Map<String, TStoreMgmt> storeMap = storeList.stream()
                .collect(Collectors.toMap(TStoreMgmt::getId,store -> store,(existing, replacement) -> existing)); // 处理键冲突，保留已有值

        // 处理账单号映射
        Map<String, TStoreMgmt> billNoMap = storeList.stream()
                .filter(store -> StringUtils.isNotBlank(store.getBillNo()))
                .collect(Collectors.toMap(TStoreMgmt::getBillNo,store -> store,(existing, replacement) -> existing)); // 处理键冲突，保留已有值

        // 存储到Redis
        for (Map.Entry<String, TStoreMgmt> entry : storeMap.entrySet()) {
            RedisCacheUtils.setCacheObject(CacheConstants.STORE_REDIS_KEY + entry.getKey(), entry.getValue());
        }

        for (Map.Entry<String, TStoreMgmt> entry : billNoMap.entrySet()) {
            RedisCacheUtils.setCacheObject(CacheConstants.STORE_REDIS_KEY + entry.getKey(), entry.getValue());
        }
    }

    @Override
    public SysUser getUserByStoreId(String storeId) {
        return toreMgmtMapper.getUserByStoreId(storeId);
    }

    @Override
    public List<StoreLeaderRespVo> getStoreLeaderList() {
        return toreMgmtMapper.getStoreLeaderList();
    }
}
